/**
 * 
 */
package com.yidu.drug.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.yidu.drug.action.vo.ManufacturePlanModelVo;
import com.yidu.drug.common.Page;
import com.yidu.drug.common.Tools;
import com.yidu.drug.dao.DrugMaterialsModelMapper;
import com.yidu.drug.dao.DrugModelMapper;
import com.yidu.drug.dao.DrugProductionModelMapper;
import com.yidu.drug.dao.ManufacturePlanModelMapper;
import com.yidu.drug.dao.ProductionModelMapper;
import com.yidu.drug.model.DrugModel;
import com.yidu.drug.model.DrugProductionModel;
import com.yidu.drug.model.ManufacturePlanModel;
import com.yidu.drug.model.ProductionModel;
import com.yidu.drug.service.ManufacturePlanService;

/**
 * 王志伟制造计划实现类
 * @author 王志伟
 * @date 2018年9月5日 下午7:40:54
 * @version 1.0
 */
@Service
public class ManufacturePlanServiceImpl implements ManufacturePlanService{

	//制造计划DAO
	@Resource
	private ManufacturePlanModelMapper mfpDao;
	
	//生产计划DAO
	@Resource
	private ProductionModelMapper proDao;
	
	//药品DAO
	@Resource
	private DrugModelMapper drugDao;
	
	//药品生产DAO
	@Resource
	private DrugProductionModelMapper dpDao;
	
	
	//药品材料DAO
	@Resource
	private DrugMaterialsModelMapper dmDao;
	
	@Override
	public int deleteByPrimaryKey(String mpId) {
		//根据ID删除
		return mfpDao.deleteByPrimaryKey(mpId);
	}

	@Override
	public int insert(ManufacturePlanModel record) {
		//加入制造计划
		return mfpDao.insert(record);
	}

	@Override
	public int insertSelective(ManufacturePlanModel record) {
		//加入制造计划
		return mfpDao.insertSelective(record);
	}

	@Override
	public ManufacturePlanModel selectByPrimaryKey(String mpId) {
		//查询所有 
		return mfpDao.selectByPrimaryKey(mpId);
	}

	@Override
	public int updateByPrimaryKeySelective(ManufacturePlanModel record) {
		//修改制造计划
		return mfpDao.updateByPrimaryKeySelective(record);
	}

	@Override
	public int updateByPrimaryKey(ManufacturePlanModel record) {
		//修改制造计划
		return mfpDao.updateByPrimaryKeySelective(record);
	}

	@Override
	public Map<String, Object> findAll(String limit, String page, String proId, String drugId) {
		//NEW一个制造的模型,加入页面信息
		ManufacturePlanModel mfp = new ManufacturePlanModel();
		Page pa=new Page();
		pa.setPage(Integer.valueOf(page));
		pa.setRows(Integer.valueOf(limit));
		mfp.setPage(pa);
		mfp.setProId(proId);
		mfp.setDrugId(drugId);
		//查询所有信息或者按条件查询
		List<ManufacturePlanModel> list=mfpDao.findAll(mfp);
		for (ManufacturePlanModel manufacturePlanModel : list) {
			String proIds=manufacturePlanModel.getProId();
			ProductionModel pro=proDao.selectByPrimaryKey(proIds);
			manufacturePlanModel.setEntName(pro.getEntName());
			
			String drugIds=manufacturePlanModel.getDrugId();
			DrugModel drug=drugDao.selectByPrimaryKey(drugIds);
			manufacturePlanModel.setDrugName(drug.getDrugName());
		}
		//查询出的信息的总行数
		Long count = mfpDao.findRows(mfp);
		//加入MAP集合
		Map<String,Object> map = new HashMap<String, Object>();
		map.put("code",0);
		map.put("msg","");
		map.put("count",count);
		map.put("data", list);
		return map;
	}
	
	@Override
	public List<ProductionModel> findAllsProduction() {
		//查询所有的生产计划
		return proDao.findAllsProduction();
	}

	@Override
	public List<DrugModel> findAllsDrug() {
		//查询所有药品
		return drugDao.findAllsDrug();
	}

	@Override
	public Map<String, Object> findAllss(String proId) {
		List<ManufacturePlanModelVo> mfpList=new ArrayList<ManufacturePlanModelVo>();
		List<DrugProductionModel> dpList=dpDao.findDrugIdByProId(proId);
		for(int i=0; i<dpList.size(); i++){
			ManufacturePlanModel mfp=mfpDao.findByProIdAndDrugIdAndMaxSort(proId,dpList.get(i).getDrugId());
			if(mfp!=null){
				if(!mfp.getMpUnfinished().equals("0")){
					System.out.println("对象不不不不为空============");
					System.out.println("计划生产数量"+mfp.getMpTask());
					System.out.println("未完成的数量"+mfp.getMpUnfinished());
					//获取药品ID
					String drugId=mfp.getDrugId();
					//获取药品名称
					DrugModel drug=drugDao.selectByPrimaryKey(drugId);
					String drugName=drug.getDrugName();
					//获取生产厂家
					ProductionModel pro=proDao.selectByPrimaryKey(proId);
					String entName=pro.getEntName();
					//加入制造计划VO表
					ManufacturePlanModelVo mfpVo=new ManufacturePlanModelVo();
					mfpVo.setMpTask(mfp.getMpUnfinished());
					mfpVo.setProId(proId);
					mfpVo.setDrugId(drugId);
					mfpVo.setDrugName(drugName);
					mfpVo.setEntName(entName);
					mfpVo.setMpUnfinished("0");
					mfpVo.setSort(mfp.getSort()+1);
					mfpVo.setPwId(Tools.getRandomUUID());
					mfpVo.setId(Tools.getRandomUUID());
					mfpVo.setMpFinished("未完成");
					mfpVo.setMpScheme("继续制造");
					mfpList.add(mfpVo);
				}
			}
			if(mfp==null){
				System.out.println("对象为空============");
				//获取药品ID
				String drugId=dpList.get(i).getDrugId();
				//获取药品名称
				DrugModel drug=drugDao.selectByPrimaryKey(drugId);
				String drugName=drug.getDrugName();
				//获取生产厂家
				ProductionModel pro=proDao.selectByPrimaryKey(proId);
				String entName=pro.getEntName();
				ManufacturePlanModelVo mfpVo=new ManufacturePlanModelVo();
				mfpVo.setMpTask(String.valueOf(dpList.get(i).getDrpNumber()));
				mfpVo.setDrugId(drugId);
				mfpVo.setProId(proId);
				mfpVo.setDrugName(drugName);
				mfpVo.setMpUnfinished("0");
				mfpVo.setEntName(entName);
				mfpVo.setSort(1);
				mfpVo.setPwId(Tools.getRandomUUID());
				mfpVo.setId(Tools.getRandomUUID());
				mfpVo.setMpFinished("未完成");
				mfpVo.setMpScheme("继续制造");
				mfpList.add(mfpVo);
			}
			
		}
		Map<String,Object> map = new HashMap<String, Object>();
		map.put("code",0);
		map.put("data", mfpList);
		return map;
	}

	/**
	 * 增加制造计划
	 */
	@Override
	public int mfpAdd(String a) {
		Gson gosn=new Gson();
		//使用Type类，取得相应类型对象的class属性
		java.lang.reflect.Type listType=new TypeToken<ArrayList<ManufacturePlanModelVo>>(){}.getType();//TypeToken内的泛型就是Json数据中的类型
		//使用集合接受将json转换为java对象集合后的数据
		ArrayList<ManufacturePlanModelVo> list = gosn.fromJson(a, listType);
		int row=0;
		int js=0;
		for(int i=0; i<list.size(); i++){
			System.out.println(list.get(i).getDrugName()+"药品名字");
			System.out.println(list.get(i).getMpFinished()+"是否完成");
			ManufacturePlanModel mfp=new ManufacturePlanModel();
			mfp.setMpId(Tools.getRandomUUID());
			mfp.setProId(list.get(i).getProId());
			Date date=new Date();
			mfp.setMpTime(date);
			mfp.setMpTask(list.get(i).getMpTask());
			mfp.setMpFinished(list.get(i).getMpFinished());
			//计划数量
			int unNum=Integer.valueOf(list.get(i).getMpTask());
			//完成的数量
			int onNum=Integer.valueOf(list.get(i).getMpUnfinished());
			//未完成的数量
			String mpUnfinished=String.valueOf(unNum-onNum);
			mfp.setMpUnfinished(mpUnfinished);
			mfp.setMpScheme(list.get(i).getMpScheme());
			mfp.setIsva(Tools.ISVA_YES);
			mfp.setSort(Integer.valueOf(list.get(i).getSort()));
			mfp.setDrugId(list.get(i).getDrugId());
			row=mfpDao.insertSelective(mfp);
			if(unNum==onNum){
				js=js+1;
			}
		}
		if(js==list.size()){
			ProductionModel pro=new ProductionModel();
			pro.setProId(list.get(0).getProId());
			pro.setOperator("制造完成");
			proDao.updateByPrimaryKeySelective(pro);
		}
		if(js<list.size()){
			ProductionModel pro=new ProductionModel();
			pro.setProId(list.get(0).getProId());
			pro.setOperator("正在制造");
			proDao.updateByPrimaryKeySelective(pro);
		}
		
		return row;
	} 
	
}
